package com.yyfax.pay.common.constants;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.yyfax.commons.lang.DateUtil;

/**
 * 支付模块常量配置
 *
 * @author buyi
 * @date 2017-04-14 10:47:44
 * @since v1.0.0
 */
@Component
public class PayConstants {

	/**
	 * 签名串字段关键字
	 */
	public static final String FIELD_SIGN = "sign";
	/**
	 * 商户号字段关键字
	 */
	public static final String FIELD_MERCHANT_CODE = "merchantCode";

	/**
	 * 姓名最大长度
	 */
	public static final int NAME_MAX_LENGTH = 15;

	/**
	 * 手机号码缓存时间，单位秒
	 */
	public static final int PHONE_CACHE_EXPIRE_SECOND = 120;

	/**
	 * 连接默认超时时间，单位毫秒
	 */
	public static final int DEFAULT_CONNECT_TIMEOUT = 30000;
	/**
	 * 读取默认超时时间，单位毫秒
	 */
	public static final int DEFAULT_READ_TIMEOUT = 30000;

	/**
	 * 支付模块上下文路径 关键字
	 */
	@Value(value = "${pay.config.context_path_key}")
	public String ctx = "ctx";
	/**
	 * 支付对外域名（生产环境外部服务走https）
	 */
	private String domain = "http://localhost";

	/**
	 * 支付对内域名（生产环境内部服务走http）
	 */
	private String innerDomain = "http://localhost";

	/**
	 * 收银台路径
	 */
	private String counterUrl = "/web/pay/counter";
	/**
	 * 同步通知路径
	 */
	private String returnUrl = "/api/result/pay/return/%s";
	/**
	 * 异步通知路径
	 */
	private String notifyUrl = "/api/result/pay/notify/%s";

	/**
	 * 签名密钥
	 */
	private String signKey;

	/**
	 * 订单默认失效时间 ,单位毫秒，默认10分钟
	 */
	private int defaultExpireTime = 600000;

	/**
	 * 支付超时/挂起时，主动查单的时间延迟，单位分
	 */
	private int payQueryMinuteDelay = 15;

	/**
	 * 批量代付查询，查询结果时间延迟
	 */
	private int payoutBatchQueryMinuteDelay = 15;

	/**
	 * 批量代付对账，延迟天数
	 */
	private int payoutBatchCheckDayDelay = 1;

	/**
	 * 批量代付对账，查询时
	 */
	private int payoutBatchCheckHour = 9;

	/**
	 * 批量代付对账，查询分
	 */
	private int payoutBatchCheckMinute = 0;

	/**
	 * 批量代付对账，查询秒
	 */
	private int payoutBatchCheckSecond = 0;

	/**
	 * 获取验证码CD时长
	 */
	private int sendVerifyCoolDownTime = 120;

	/**
	 * 通知最大次数
	 */
	private int maxNo = 6;

	/**
	 * 下次执行间隔，值用逗号分开
	 */
	private String nextTimeInterval;

	/**
	 * 默认下次执行时间间隔，单位秒
	 */
	private static final int DEFAULT_NEXT_TIME_INTERVAL = 3600;

	private int queryTaskMax = 5;
	/**
	 * 查询间隔倍数
	 */
	private int queryTaskTime = 60;

	/**
	 * 异步通知开始时间延迟
	 */
	@Value("${task.config.notify_start_time_delay:10}")
	private int notifyStartTimeDelay;

	/**
	 * 批量文件最大限制
	 */
	@Value("${pay.file_config.batch_max_size:100}")
	private int fileBatchMaxSize;


	public static final String OPEN_ACCOUNT_SUCCESS = "开户成功";

	public PayConstants( //
						 @Value(value = "${pay.config.domain}") String domain //
			, @Value(value = "${pay.config.inner}") String innerDomain //
			, @Value(value = "${pay.config.sign_key}") String signKey //
			, @Value(value = "${order.config.default_expire_time}") int defaultExpireTime //
			, @Value(value = "${order.config.pay_query_minute_delay}") int payQueryMinuteDelay //
			, @Value(value = "${order.config.payout_batch_query_minute_delay}") int payoutBatchQueryMinuteDelay //
			, @Value(value = "${order.config.payout_batch_check_day_delay}") int payoutBatchCheckDayDelay //
			, @Value(value = "${order.config.payout_batch_check_hour}") int payoutBatchCheckHour //
			, @Value(value = "${order.config.payout_batch_check_minute}") int payoutBatchCheckMinute //
			, @Value(value = "${order.config.payout_batch_check_second}") int payoutBatchCheckSecond //
			, @Value(value = "${pay.config.send_verify_cool_down_time}") int sendVerifyCoolDownTime //
			, @Value(value = "${query_task.time}") int queryTaskTime //
			, @Value(value = "${query_task.max}") int queryTaskMax//
			, @Value(value = "${task.config.next_time_interval}") String nextTimeInterval) {
		this.domain = domain;
		this.innerDomain = innerDomain;

		this.counterUrl = this.domain.concat(counterUrl);
		this.returnUrl = this.domain.concat(returnUrl);
		this.notifyUrl = this.domain.concat(notifyUrl);

		this.signKey = signKey;

		this.defaultExpireTime = defaultExpireTime;

		this.payQueryMinuteDelay = payQueryMinuteDelay;
		this.payoutBatchQueryMinuteDelay = payoutBatchQueryMinuteDelay;
		this.payoutBatchCheckDayDelay = payoutBatchCheckDayDelay;
		this.payoutBatchCheckHour = payoutBatchCheckHour;
		this.payoutBatchCheckMinute = payoutBatchCheckMinute;
		this.payoutBatchCheckSecond = payoutBatchCheckSecond;

		this.sendVerifyCoolDownTime = sendVerifyCoolDownTime;

		this.queryTaskTime = queryTaskTime;
		this.queryTaskMax = queryTaskMax;

		this.nextTimeInterval = nextTimeInterval;
	}

	public String getCtx() {
		return ctx;
	}

	public void setCtx(String ctx) {
		this.ctx = ctx;
	}

	public String getCounterUrl() {
		return counterUrl;
	}

	public void setCounterUrl(String counterUrl) {
		this.counterUrl = counterUrl;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public String getReturnUrl() {
		return returnUrl;
	}

	public void setReturnUrl(String returnUrl) {
		this.returnUrl = returnUrl;
	}

	public String getNotifyUrl() {
		return notifyUrl;
	}

	public void setNotifyUrl(String notifyUrl) {
		this.notifyUrl = notifyUrl;
	}

	public String getSignKey() {
		return signKey;
	}

	public void setSignKey(String signKey) {
		this.signKey = signKey;
	}

	public int getDefaultExpireTime() {
		return defaultExpireTime;
	}

	public void setDefaultExpireTime(int defaultExpireTime) {
		this.defaultExpireTime = defaultExpireTime;
	}

	public int getPayQueryMinuteDelay() {
		return payQueryMinuteDelay;
	}

	public void setPayQueryMinuteDelay(int payQueryMinuteDelay) {
		this.payQueryMinuteDelay = payQueryMinuteDelay;
	}

	public int getPayoutBatchQueryMinuteDelay() {
		return payoutBatchQueryMinuteDelay;
	}

	public void setPayoutBatchQueryMinuteDelay(int payoutBatchQueryMinuteDelay) {
		this.payoutBatchQueryMinuteDelay = payoutBatchQueryMinuteDelay;
	}

	public int getPayoutBatchCheckDayDelay() {
		return payoutBatchCheckDayDelay;
	}

	public void setPayoutBatchCheckDayDelay(int payoutBatchCheckDayDelay) {
		this.payoutBatchCheckDayDelay = payoutBatchCheckDayDelay;
	}

	public int getPayoutBatchCheckHour() {
		return payoutBatchCheckHour;
	}

	public void setPayoutBatchCheckHour(int payoutBatchCheckHour) {
		this.payoutBatchCheckHour = payoutBatchCheckHour;
	}

	public int getPayoutBatchCheckMinute() {
		return payoutBatchCheckMinute;
	}

	public void setPayoutBatchCheckMinute(int payoutBatchCheckMinute) {
		this.payoutBatchCheckMinute = payoutBatchCheckMinute;
	}

	public int getPayoutBatchCheckSecond() {
		return payoutBatchCheckSecond;
	}

	public void setPayoutBatchCheckSecond(int payoutBatchCheckSecond) {
		this.payoutBatchCheckSecond = payoutBatchCheckSecond;
	}

	public int getSendVerifyCoolDownTime() {
		return sendVerifyCoolDownTime;
	}

	public void setSendVerifyCoolDownTime(int sendVerifyCoolDownTime) {
		this.sendVerifyCoolDownTime = sendVerifyCoolDownTime;
	}

	public int getMaxNo() {
		return maxNo;
	}

	public void setMaxNo(int maxNo) {
		this.maxNo = maxNo;
	}

	public int getQueryTaskMax() {
		return queryTaskMax;
	}

	public void setQueryTaskMax(int queryTaskMax) {
		this.queryTaskMax = queryTaskMax;
	}

	public int getQueryTaskTime() {
		return queryTaskTime;
	}

	public void setQueryTaskTime(int queryTaskTime) {
		this.queryTaskTime = queryTaskTime;
	}

	public String getReturnUrl(String paymentCode) {
		return String.format(getReturnUrl(), paymentCode);
	}

	public String getNotifyUrl(String paymentCode) {
		return String.format(getNotifyUrl(), paymentCode);
	}

	public String getNextTimeInterval() {
		return nextTimeInterval;
	}

	public void setNextTimeInterval(String nextTimeInterval) {
		this.nextTimeInterval = nextTimeInterval;
	}

	public int getNotifyStartTimeDelay() {
		return notifyStartTimeDelay;
	}

	public void setNotifyStartTimeDelay(int notifyStartTimeDelay) {
		this.notifyStartTimeDelay = notifyStartTimeDelay;
	}

	public int getFileBatchMaxSize() {
		return fileBatchMaxSize;
	}

	public void setFileBatchMaxSize(int fileBatchMaxSize) {
		this.fileBatchMaxSize = fileBatchMaxSize;
	}

	public String getInnerDomain() {
		return innerDomain;
	}

	public void setInnerDomain(String innerDomain) {
		this.innerDomain = innerDomain;
	}

	/**
	 * 获取下一次执行的时间间隔，从0开始
	 *
	 * @param index
	 * @return
	 * @author buyi
	 * @date 2017-09-04 14:04:11
	 * @since v2.0.0
	 */
	public int getNextTimeInterval(int index) {
		if (StringUtils.isBlank(this.nextTimeInterval)) {
			throw new IllegalArgumentException("任务执行间隔么有配置task.config.next_time_interval");
		}

		String[] nextTimeIntervals = this.nextTimeInterval.split(",");
		if (nextTimeIntervals.length - 1 < index) {
			return DEFAULT_NEXT_TIME_INTERVAL;
		}

		return Integer.valueOf(nextTimeIntervals[index]);
	}

	/**
	 * 获取下一次执行时间，从0开始
	 *
	 * @param index
	 * @return
	 * @author buyi
	 * @date 2017-09-04 14:08:02
	 * @since v2.0.0
	 */
	public String getNextTime(int index) {

		int second = getNextTimeInterval(index);

		return DateUtil.datetime2Str(DateUtil.addSecond(new Date(), second));

	}
}
